#include "WidgetGrabber.h"
#include <QMouseEvent>
#include <QKeyEvent>
#include <QApplication>
#include <QDebug>
#include <QWidget>
#include <QPainter>
#include <QToolTip>

WidgetGrabber::WidgetGrabber(QObject* parent) : QObject(parent)
{
    mTimer.setInterval(100);
    connect(&mTimer, SIGNAL(timeout()), this, SLOT(grab()));
    mTimer.start();
    qDebug() << "WidgetGrabber::WidgetGrabber";
}

void WidgetGrabber::grab()
{
    QWidget* widget = QApplication::widgetAt(QCursor::pos());
    //qDebug() << "WidgetGrabber::grab QApplication::widgetAt " << widget;

    if(widget)
    {
        QString display = QString("%1(%2)").arg(widget->metaObject()->className(), widget->objectName());


        QString ancestors("");

        QObject* ancestor = widget;

        while(ancestor->parent())
        {
            ancestors.append(" -> ");
            ancestor = ancestor->parent();
            ancestors.append(QString("%1(%2)").arg(ancestor->metaObject()->className(), ancestor->objectName()));
            //(ancestor->objectName());
        }

        display.append(ancestors);

        QToolTip::showText(QCursor::pos(), display);

        //widget->metaObject()->className();
    }
}
